import React, { Component } from 'react';
import store from './redux/store';
import { add, sub, ddAdd, oddAdd, jishuAdd } from "./redux/count_antion"
class One extends Component {
    state = {
        a: 'hahah',
        b: "xxxxx"
    }
    sel;
    add = () => {
        store.dispatch(add(Number(this.sel.value)))
    }
    sub = () => {
        store.dispatch(sub(Number(this.sel.value)))
    }
    oddAdd = () => {
        let count = store.getState()
        if (count % 2 === 0) {
            store.dispatch(oddAdd(Number(this.sel.value)))
        }

    }
    jishuAdd = () => {
        let count = store.getState()
        if (count % 2 !== 0) {
            store.dispatch(jishuAdd(Number(this.sel.value)))
        }
    }
    ddAdd = () => {
        setTimeout(() => {
            store.dispatch(ddAdd(Number(this.sel.value)))
        }, 1000)
    }
    componentDidMount() {
        store.subscribe(() => {
            this.setState({})
        })
    }
    render() {
        return (
            <div>
                当前count的值为：{store.getState()} &nbsp;
                <select ref={e => this.sel = e}>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select> <br />
                <button onClick={this.add}>加</button> &nbsp;
                <button onClick={this.sub}>减</button> &nbsp;
                <button onClick={this.oddAdd}>偶数加一</button> &nbsp;
                <button onClick={this.jishuAdd}>奇数加一</button> &nbsp;
                <button onClick={this.ddAdd}>等一等加</button> &nbsp;
            </div >
        );
    }
}

export default One;